package net.bolue.web.util;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


public class WriterUtil {

	public static void outputString(HttpServletResponse response,
			String jsonString) throws IOException {
		response.setContentType("text/html");// html/txt
		response.setCharacterEncoding("utf-8");
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache, must-revalidate");
		response.setHeader("Pragma", "no-cache");
		PrintWriter out = response.getWriter();
		out.write(jsonString);
		out.flush();
		out.close();
	}
	
	public static void getJSONString(HttpServletResponse response,
			String jsonString) throws IOException {
		response.setContentType("text/html");// html/txt mime
		response.setCharacterEncoding("utf-8");
		response.setHeader("Cache-Control", "no-cache, must-revalidate");
		PrintWriter out = response.getWriter();
		out.write(jsonString);
		out.flush();
		out.close();
	}
	
	public static void outPutObject(Object o,HttpServletResponse response){
		JSONObject jsonObject = JSONObject.fromObject(o);
		String jsonString = jsonObject.toString();
		try {
			getJSONString(response, jsonString);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void outPutMap(Map<String,Object> map,HttpServletResponse response){
		JSONObject jsonObject = JSONObject.fromObject(map);
		String jsonString = jsonObject.toString();
		try {
			getJSONString(response, jsonString);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void outPutList(List list,HttpServletResponse response){
		JSONArray jsonArray = JSONArray.fromObject(list);
		String jsonString = jsonArray.toString();
		try {
			WriterUtil.getJSONString(response, jsonString);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 用于以XML格式输出XML字符串
	 * 
	 * @param strXml
	 *            XML字符串
	 * @param encoding
	 *            字符编码
	 * @param response
	 */
	public static void outPutXML(String strXml, String encoding,
			HttpServletResponse response) {
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setCharacterEncoding(encoding);
		response.setContentType("application/xml");// 设置输出流格式为XML
		PrintWriter out;
		try {
			out = response.getWriter();
			out.write(strXml);
			out.flush();
			out.close();

		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	
	public static void outputExcel(File fileName , String strExcel,
			HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("utf-8"); 
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		response.setHeader("Content-disposition","attachment;filename=" + fileName);
		//excel内容		String strExcel = "";
		PrintWriter outW= response.getWriter();
		outW.write(strExcel);
		outW.flush();
		outW.close();
	}
	
}
